package com.bw.zh

import android.app.Dialog
import android.content.Intent
import android.os.Bundle
import android.text.SpannableString
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.bw.zg2.base.BaseActivity
import com.bw.zg2.utils.GlideUtil
import com.bw.zg2.utils.SpUtil
import com.bw.zg2.viewmodel.HomeViewModel
import com.bw.zh.bean.SecondItemBean
import com.bw.zh.databinding.ActivityMainBinding
import com.bw.zh.view.WebViewActivity
import com.bw.zh.view.WebViewActivity2
import com.bw.zh.view.adapter.SecondHAdapter

class MainActivity : BaseActivity<HomeViewModel, ActivityMainBinding>(R.layout.activity_main) {

    lateinit var adapter: SecondHAdapter
    val data = mutableListOf<SecondItemBean>()


    override fun initData() {

        viewModel.secondLiveData.observe(this) {
            data.addAll(it.result.list)
            adapter.notifyDataSetChanged()
        }

    }

    override fun initView(savedInstanceState: Bundle?) {
        viewModel.getSecond()

        dataBinding.rvSecond.layoutManager = LinearLayoutManager(this)
        adapter = SecondHAdapter(this, R.layout.item_second_layout, data)
        dataBinding.rvSecond.adapter = adapter


        //获取弹框显示flag ； flag！=1，需要显示弹框
        if (SpUtil.getInt("show") != 1) {
            val dialog = Dialog(this)
            dialog.setContentView(R.layout.dialog_layout)
            val tvUser = dialog.findViewById<TextView>(R.id.tv_user)
            val tvYs = dialog.findViewById<TextView>(R.id.tv_ys)

            val t = "选择【同意】即表示充分阅读、理解并接受房网《用户协议》与《隐私协议》"
            val sp = SpannableString(t)

            val color = ContextCompat.getColor(this, R.color.yellow)
            sp.setSpan(
                ForegroundColorSpan(color),
                13,
                t.length,
                SpannableString.SPAN_INCLUSIVE_INCLUSIVE
            )

//        tvContent3.text = sp.toString()
            val btnNo = dialog.findViewById<TextView>(R.id.btn_no)
            val btnYes = dialog.findViewById<TextView>(R.id.btn_yes)
            tvUser.setOnClickListener {
                val intent = Intent(this@MainActivity, WebViewActivity::class.java)
                intent.putExtra("url", "https://m.qfang.com/shenzhen/user/user-protocol.html")
                startActivity(intent)
            }
            tvYs.setOnClickListener {
                val intent = Intent(this@MainActivity, WebViewActivity::class.java)
                intent.putExtra("url", "https://m.qfang.com/shenzhen/user/privacy-protocol.html")
                startActivity(intent)
            }

            btnNo.setOnClickListener {
                dialog.dismiss()
            }

            btnYes.setOnClickListener {
                dialog.dismiss()
                //点击同意，保存弹框显示flag = 1
                SpUtil.putInt("show", 1)
            }
            dialog.show()
        }

    }

}